/* 
 * Quechua - the lightweight data mining framework
 *
 * Copyright (C) 2012 Marek Denis <quechua@octogan.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../include/interface-algorithm.h"

Algorithm::Algorithm() : internal(new Internal)  {
    if(internal==NULL) {
        LOG(CRITIC) << "Algorithm::internal is not initialized, exiting";
        return;
    }
    internal->type = ALGORITHM;
};

Algorithm::~Algorithm() {
    if(internal!=NULL)
        delete internal;
};

bool Algorithm::prepare() {
    return true;
};

bool Algorithm::start() {
    return true;
};
bool Algorithm::stop() {
    return true;
};

bool Algorithm::reset() {
    return true;
};

bool Algorithm::load_misc_conf(const setting_t& misc) {
    return true;
};

datapack_ptr serve_algorithm(datapack_ptr data, void* object, STEP step) {
    Algorithm* const algorithm = reinterpret_cast<Algorithm* const>(object);
    datapack_ptr algorithm_result; // this must be evaluated by operator() method
    if(step!=ALGRUNNING){
        LOG(CRITIC) << "Trying to serve algorithm object in a separated thread "
                    << "while step is " << step << ", exiting from algorithm serving";
        return algorithm_result; // empty shared ptr
    }
    LOG(VERBOSE) << "algorithm->work"; // to be erased

    try {
        algorithm_result = algorithm->work(data);
    } catch(std::bad_cast& ex) {
       LOG(ERROR) << "While running Algorithm method, bad cast occured, probably DataPack is not what Algorithm expected to be";

    }
    algorithm_result->pass_stamp(data);

    return algorithm_result;
};

